#!/bin/sh

# Copyright (C)2004 Landmark Graphics Corporation
# Copyright (C)2005 Sun Microsystems, Inc.
# Copyright (C)2009-2016 D. R. Commander
#
# This library is free software and may be redistributed and/or modified under
# the terms of the wxWindows Library License, Version 3.1 or (at your option)
# any later version.  The full license is in the LICENSE.txt file included
# with this distribution.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# wxWindows Library License for more details.

# The interpreter to use (if this script is submitted to N1 Grid Engine):
#$ -S /bin/sh
#
# This allocates a graphics device and passes it in the VGL_DISPLAY
# environment:
#$ -l gfx=1
#
# Specify other environment variables that N1GE should pass to this script:
#$ -v DISPLAY
#$ -v SSH_CLIENT
#$ -v VGL_CLIENT
#$ -v VGL_COMPRESS
#$ -v VGL_PORT
#$ -v VGL_SSL
#$ -v VGL_GLLIB
#$ -v VGL_X11LIB
#$ -v VGL_GAMMA
#$ -v VGL_PROGRESSIVE
#$ -v VGL_NPROCS

usage()
{
	echo
	echo "Usage: `basename $0` [options] [--] <OpenGL app> [OpenGL app arguments]"
	echo
	echo "VirtualGL options (see documentation for a more comprehensive list)"
	echo
	echo "-c <c>    : proxy = Send 3D images uncompressed using X11 Transport"
	echo "                    [default for local X connections]"
	echo "            jpeg = Compress 3D images using JPEG/send using VGL Transport"
	echo "                   [default for remote X connections]"
	echo "            rgb = Encode 3D images as RGB/send using VGL Transport"
	echo "            xv = Encode 3D images as YUV420P/send using XV Transport"
	echo "            yuv = Encode 3D images as YUV420P/send using the VGL Transport"
	echo "                  and display on the client using X Video"
	echo "            [If an image transport plugin is being used, then <c> can be any"
	echo "             number >= 0 (default=0).]"
	echo
	echo "-nodl     : Don't interpose the dlopen() function.  dlopen() is normally"
	echo "            interposed to force applications that use dlopen() to load libGL"
	echo "            to load VirtualGL instead.  For the more common case of"
	echo "            applications that link directly with libGL, disabling the dlopen()"
	echo "            interposer makes VirtualGL less intrusive, since it will no longer"
	echo "            load libGL until the application actually uses that library."
	echo
	echo "-d <d>    : <d> = the X Display to use for 3D rendering [default = :0.0]"
	echo
	echo "-fps <f>  : Limit client/server frame rate to <f> frames/sec"
	echo
	echo "-gamma <g>: Set gamma correction factor to <g> (see docs)"
	echo
	echo "-ge       : Fool application into thinking that LD_PRELOAD is unset"
	echo
	echo "-ms <s>   : Force VirtualGL to use OpenGL multisampling with <s>"
	echo "            samples (<s> = 0 forces multisampling to be disabled)"
	echo
	echo "-np <n>   : Use <n> CPUs to perform image compression [default = 1]"
	echo
	echo "+/-pr     : Enable/disable performance profiling output [default = disabled]"
	echo
	echo "-q <q>    : Compression quality [1 <= <q> <= 100]"
	echo "            [default = 95 for JPEG/VGL Transport.  Has no effect with"
	echo "             X11 Transport]"
	echo
	echo "-samp <s> : Chrominance subsampling factor"
	echo "            <s> = gray, 1x, 2x, 4x"
	echo "            [default = 1x for JPEG/VGL Transport.  Has no effect with"
	echo "             X11 Transport or RGB encoding]"
	echo
	echo "+/-s      : Enable/disable SSL encryption of VGL Transport or custom"
	echo "            image transport, if applicable."
	echo "            [default = disabled.  Has no effect on the VGL Transport unless"
	echo "             VirtualGL was built with OpenSSL support]"
	echo
	echo "+/-sp     : Turn on/off frame spoiling [default = enabled]"
	echo
	echo "-st <s>   : left = Send only left eye buffer of stereo application"
	echo "            right = Send only right eye buffer of stereo application"
	echo "            quad = Use quad-buffered stereo if available, otherwise use"
	echo "                   red/cyan (anaglyphic) stereo [default]"
	echo "            rc = Always use red/cyan (anaglyphic) stereo"
	echo "            gm = Always use green/magenta (anaglyphic) stereo"
	echo "            by = Always use blue/yellow (anaglyphic) stereo"
	echo "            i = Always use interleaved (passive) stereo"
	echo "            tb = Always use top/bottom (passive) stereo"
	echo "            ss = Always use side-by-side (passive) stereo"
	echo
	echo "+/-sync   : Enable/disable strict 2D/3D synchronization [default = disabled]"
	echo
	echo "+/-tr     : Enable/disable function call tracing (generates a lot of output)"
	echo "            [default = disabled]"
	echo
	echo "-trans <t>: Use transport plugin contained in library libvgltrans_<t>.so"
	echo
	echo "+/-v      : Enable/disable verbose VirtualGL messages [default = disabled]"
	echo
	echo "+xcb/-xcb : Enable/disable XCB interposer [default = enabled]"
	echo
	echo "+wm/-wm   : Enable/disable window manager mode (for running compiz, etc.)"
	echo
	echo "-64       : (Solaris only) Preload VirtualGL only into 64-bit apps"
	echo "-32       : (Solaris only) Preload VirtualGL only into 32-bit apps"
	echo
	exit $1
}

GEOPROBE_USEGLX=1
export GEOPROBE_USEGLX
PROMAGIC_USEGLX=1
export PROMAGIC_USEGLX
VBOX_CROGL_FORCE_SUPPORTED=1
export VBOX_CROGL_FORCE_SUPPORTED

__VGL_DL=1
__VGL_GE=0
VGL_LIBRARY_PATH=
MYPATH=`dirname $0`

while [ $# -gt 0 ]
do
	case "$1" in
	+sy*)       VGL_SYNC=1        ; export VGL_SYNC     ;;
	-sy*)       VGL_SYNC=0        ; export VGL_SYNC     ;;
	-st*)       VGL_STEREO=$2     ; export VGL_STEREO   ; shift     ;;
	+sp*)       VGL_SPOIL=1       ; export VGL_SPOIL    ;;
	-sp*)       VGL_SPOIL=0       ; export VGL_SPOIL    ;;
	-sa*)       VGL_SUBSAMP=$2    ; export VGL_SUBSAMP  ; shift     ;;
	-s*)        VGL_SSL=0         ; export VGL_SSL      ;;
	+s*)        VGL_SSL=1         ; export VGL_SSL      ;;
	-prog*)     VGL_PROGRESSIVE=0 ; export VGL_PROGRESSIVE  ;;
	+prog*)     VGL_PROGRESSIVE=1 ; export VGL_PROGRESSIVE  ;;
	-pr*)       VGL_PROFILE=0     ; export VGL_PROFILE  ;;
	+pr*)       VGL_PROFILE=1     ; export VGL_PROFILE  ;;
	-q*)        VGL_QUAL=$2       ; export VGL_QUAL     ; shift     ;;
	+de*)       VGL_DEBUG=1       ; export VGL_DEBUG    ;;
	-de*)       VGL_DEBUG=0       ; export VGL_DEBUG    ;;
	-nodl)      __VGL_DL=0        ;;
	-trans*)    VGL_TRANSPORT=$2  ; export VGL_TRANSPORT; shift     ;;
	+tr*)       VGL_TRACE=1       ; export VGL_TRACE    ;;
	-tr*)       VGL_TRACE=0       ; export VGL_TRACE    ;;
	-d*)        VGL_DISPLAY=$2    ; export VGL_DISPLAY  ; shift     ;;
	-cl*)       VGL_CLIENT=$2     ; export VGL_CLIENT   ; shift     ;;
	-p*)        VGL_PORT=$2       ; export VGL_PORT     ; shift     ;;
	-c*)        VGL_COMPRESS=$2   ; export VGL_COMPRESS ; shift     ;;
	-np*)       VGL_NPROCS=$2     ; export VGL_NPROCS   ; shift     ;;
	-gamma)     VGL_GAMMA=$2      ; export VGL_GAMMA    ; shift     ;;
	-ge)        __VGL_GE=1        ;;
	-g*)        VGL_GAMMA=0       ; export VGL_GAMMA    ;;
	+g*)        VGL_GAMMA=1       ; export VGL_GAMMA    ;;
	-v*)        VGL_VERBOSE=0     ; export VGL_VERBOSE  ;;
	+v*)        VGL_VERBOSE=1     ; export VGL_VERBOSE  ;;
	-fps)       VGL_FPS=$2        ; export VGL_FPS      ; shift     ;;
	-ms)        VGL_SAMPLES=$2    ; export VGL_SAMPLES  ; shift     ;;
	+wm)        VGL_WM=1          ; export VGL_WM       ;;
	-wm)        VGL_WM=0          ; export VGL_WM       ;;
	+xcb)       VGL_FAKEXCB=1     ; export VGL_FAKEXCB  ;;
	-xcb)       VGL_FAKEXCB=0     ; export VGL_FAKEXCB  ;;
	-ld)        VGL_LIBRARY_PATH=$2 ; shift ;;
	-h)         usage 0 ;;
	--help)     usage 0 ;;
	--)         shift   ; break ;;
	-*)         usage 1 ;;
	*)          break   ;;
	esac
	shift
done
if [ $# -eq 0 ]
then
	usage 0
fi

if [ -r /etc/opt/VirtualGL/vgl_xauth_key ]; then
	XAUTH=xauth
	if [ -x /usr/X11R6/bin/xauth ]; then
		XAUTH=/usr/X11R6/bin/xauth
	fi
	if [ -x /usr/openwin/bin/xauth ]; then
		XAUTH=/usr/openwin/bin/xauth
	fi
	$XAUTH merge /etc/opt/VirtualGL/vgl_xauth_key
fi

SSH_IP=`echo $SSH_CLIENT | cut -d' ' -f1 | cut -d: -f4`
if [ -z "$DISPLAY" ]; then
	if [ ! -z "$SSH_IP" ]; then
		DISPLAY=$SSH_IP:0.0
		export DISPLAY
		echo "[VGL] NOTICE: Automatically setting the DISPLAY environment variable to"
		echo "[VGL]    $DISPLAY, based on the IP address of your SSH client."
	fi
else
	DISPLAYHOST=`echo $DISPLAY | cut -d: -f1`
	if [ "$DISPLAYHOST" = "localhost" -o "$DISPLAYHOST" = "`hostname`" ]; then
		if [ ! -z "$SSH_IP" -a -z "$VGL_CLIENT" -a -z "$RRCLIENT" ]; then
			VGL_CLIENT=$SSH_IP:0.0
			export VGL_CLIENT
			echo "[VGL] NOTICE: Automatically setting VGL_CLIENT environment variable to"
			echo "[VGL]    $SSH_IP, the IP address of your SSH client."
		fi
	fi
fi

# This is so we can launch both 32-bit and 64-bit executables using the same
# script.
if [ -x "$MYPATH/.vglrun.vars64" ]; then
	. "$MYPATH/.vglrun.vars64"
fi
if [ -x "$MYPATH/.vglrun.vars32" ]; then
	. "$MYPATH/.vglrun.vars32"
fi
if [ ! -z "$VGL_LIBRARY_PATH" ]; then
	LD_LIBRARY_PATH=$VGL_LIBRARY_PATH:$LD_LIBRARY_PATH
	export LD_LIBRARY_PATH
	if [ ! -z "$VGL_VERBOSE" ]; then
		if [ $VGL_VERBOSE -eq 1 ]; then
			echo [VGL] NOTICE: Added $VGL_LIBRARY_PATH to LD_LIBRARY_PATH
		fi
	fi
fi

# This allows scripts to detect whether they are being launched with vglrun.
VGL_ISACTIVE=1
export VGL_ISACTIVE

SUFFIX=
if [ $__VGL_DL -eq 0 ]; then
	SUFFIX=-nodl
fi
if [ -z "$LD_PRELOAD" ]; then
	LD_PRELOAD=lib@VGL_FAKER_NAME@$SUFFIX.so
else
	LD_PRELOAD=lib@VGL_FAKER_NAME@$SUFFIX.so:$LD_PRELOAD
fi
if [ $__VGL_DL -eq 1 ]; then
	LD_PRELOAD=lib@VGL_DLFAKER_NAME@.so:$LD_PRELOAD
fi
if [ $__VGL_GE -eq 1 ]; then
	LD_PRELOAD=lib@VGL_GEFAKER_NAME@.so:$LD_PRELOAD
fi
export LD_PRELOAD
exec ${1+"$@"}
